package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.model.BaseAttrInfo;
import com.atguigu.gmall.product.model.BaseAttrValue;
import com.atguigu.gmall.product.service.BaseAttrInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: atguigu
 * @create: 2023-07-22 16:20
 */
@RestController
@RequestMapping("/admin/product")
public class BaseAttrInfoController {

    @Autowired
    private BaseAttrInfoService baseAttrInfoService;


    /**
     * 根据1,2,3级分类ID查询分类下包含平台属性，以及平台属性值
     *
     * @param category1Id
     * @param category2Id
     * @param category3Id
     * @return
     */
    @GetMapping("/attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result getAttrInfoList(
            @PathVariable("category1Id") Long category1Id,
            @PathVariable("category2Id") Long category2Id,
            @PathVariable("category3Id") Long category3Id
    ) {
        List<BaseAttrInfo> list = baseAttrInfoService.getAttrInfoList(category1Id, category2Id, category3Id);
        return Result.ok(list);
    }


    /**
     * 保存或者修改平台属性以及值
     *
     * @param baseAttrInfo
     * @return
     */
    @PostMapping("/saveAttrInfo")
    public Result saveOrUpdateAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo) {
        //1.大多数增删改业务接口一般不需响应业务数据，只要没发生异常就认为执行成功
        baseAttrInfoService.saveOrUpdateAttrInfo(baseAttrInfo);
        //2.有全局异常处理投递，业务层捕获会进行事务回滚
        return Result.ok();
    }


    /**
     * 根据平台属性ID查询属性值集合
     * @param attrId
     * @return
     */
    @GetMapping("/getAttrValueList/{attrId}")
    public Result getAttrValueList(@PathVariable("attrId") Long attrId){
        List<BaseAttrValue> list = baseAttrInfoService.getAttrValueList(attrId);
        return Result.ok(list);
    }

}
